<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <el-table
            border
            :data="deptInfos">
        <el-table-column
                prop="id"
                label="部门ID"
                >
        </el-table-column>
        <el-table-column
                prop="dname"
                label="部门名称"
                >
        </el-table-column>
        <el-table-column
                prop="dsname.state"
                label="部门状态"
                >
        </el-table-column>
        <el-table-column
                prop="docsname.state"
                label="诊断状态"
                >
        </el-table-column>
        <el-table-column
                prop=""
                >
            <template slot="header" slot-scope="scope">
                <el-button type="success" plain  @click="addDialog()">添加部门</el-button>
            </template>
            <template slot-scope="scope">
                <el-button type="success" icon="el-icon-edit" circle  @click="updateDeptInfo(scope.row.id)"></el-button>
                <!--<el-button type="danger" icon="el-icon-delete" circle @click="deleteDeptInfo(scope.row.id)"></el-button>-->
            </template>
        </el-table-column>
    </el-table>
    <el-dialog title="添加部门" :visible.sync="addDialogFormVisible">
        <el-form id="addAdminDeptFrom" :model="getDept" :rules="rules" ref="deptInfos">
            <el-form-item label="编号" prop="id" style="display: none">
                <el-input v-model="getDept.id" name="id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="dname" >
                <el-input v-model="getDept.dname" name="dname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="部门简介" prop="dintro" >
                <el-input v-model="getDept.dintro" name="dintro" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请选择部门状态" prop="state">
                <template>
                    <select style="width: 90px;" v-model="getDept.state" placeholder="请选择部门状态" name = "state">
                        <option  v-for="(g,i) in dstate"
                                 :label="g.state"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择预约状态" prop="docstate">
                <template>
                    <select style="width: 90px;" v-model="getDept.docstate" placeholder="请选择部门状态" name = "docstate">
                        <option  v-for="(g,i) in docstate"
                                 :label="g.state"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addDept()">添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改部门" :visible.sync="updateDialogFormVisible">
        <el-form id="updateAdminDeptFrom" :model="getDept" :rules="rules" ref="updateDept">
            <el-form-item label="编号" prop="id" style="display: none">
                <el-input v-model="getDept.id" name="id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="dname" >
                <el-input v-model="getDept.dname" name="dname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="部门简介" prop="dintro" >
                <el-input
                        type="textarea"
                        :rows="10"
                        placeholder="请输入内容"
                        v-model="getDept.dintro" name="dintro" autocomplete="off">
                </el-input>
            </el-form-item>
            <el-form-item label="请选择部门状态" prop="state">
                <template>
                    <select style="width: 90px;" v-model="getDept.state" placeholder="请选择部门状态" name = "state">
                        <option  v-for="(g,i) in dstate"
                                 :label="g.state"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择预约状态" prop="docstate">
                <template>
                    <select style="width: 90px;" v-model="getDept.docstate" placeholder="请选择部门状态" name = "docstate">
                        <option  v-for="(g,i) in docstate"
                                 :label="g.state"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="updateDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateDept()">修 改</el-button>
        </div>
    </el-dialog>
</div>
<script>

    $.get("/demo/dept/queryDepts",function (backData) {
        haha.deptInfos = backData.data;
    });

    var haha = new Vue({
        el: "#app",
        data: {
            docState:"",
            state:"",
            docstate:[],
            dstate:[],
            dsname:"",
            docsname:"",
            addDialogFormVisible:false,
            updateDialogFormVisible:false,
            deptInfos:[],
            getDept:{},
            rules:{
                dname: [
                    { required: true, message: '请输入部门名称', trigger: 'blur' }
                ],
                dintro: [
                    { required: true, message: '请输入部门简介（可写暂无）', trigger: 'blur' }
                ],
                state: [
                    { required: true, message: '请选择部门状态', trigger: 'blur' }
                ],
                docstate: [
                    { required: true, message: '请选择部门是否可以预约', trigger: 'blur' }
                ]
            }


        },
        methods: {
            updateDept(){
                haha.$refs["updateDept"].validate((valid) => {
                    if (valid) {
                        var updateDept = new FormData(updateAdminDeptFrom);
                        $.ajax({
                            url:"/demo/dept/updateDept",
                            type:"POST",
                            data:updateDept,
                            processData:false,
                            contentType:false,
                            success:function(backData){

                                if(backData.code == 1){
                                    haha.updateDialogFormVisible=false;
                                    haha.$message.success('修改成功');
                                    window.location.reload();
                                }else {
                                    haha.$message.error(backData.msg);
                                }
                            }
                        });
                    }
                });
            },
            updateDeptInfo(id){
                $.get("/demo/dept/queryDeptState",function (backData) {
                    haha.dstate = backData.data;
                });
                $.get("/demo/dept/queryDocState",function (backData) {
                    haha.docstate = backData.data;
                });
                $.get("/demo/dept/getDept/"+id,function (backData) {
                    haha.getDept = backData.data;
                    haha.updateDialogFormVisible = true;
                });
            },

            addDept(){
                haha.$refs["deptInfos"].validate((valid) => {
                    if (valid) {
                        var addDept = new FormData(addAdminDeptFrom);
                        $.ajax({
                            url:"/demo/dept/addDept",
                            type:"POST",
                            data:addDept,
                            processData:false,
                            contentType:false,
                            success:function(backData){
                                if(backData.code == 1){
                                    haha.addDialogFormVisible=false;
                                    haha.$message.success('添加成功');
                                    window.location.reload();
                                }else {
                                    haha.$message.error(backData.msg);
                                }
                            }
                        });
                    }
                });
            },
            addDialog(){
                $.get("/demo/dept/queryDeptState",function (backData) {
                    haha.dstate = backData.data;
                });
                $.get("/demo/dept/queryDocState",function (backData) {
                    haha.docstate = backData.data;
                });
                haha.addDialogFormVisible = true;
            },
            deleteDeptInfo(id){

                haha.$confirm('此操作将永久删除该部门, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/demo/dept/deleteDept/"+id,function (backData) {
                        if (backData.code == 1){
                            haha.$message.success('删除成功');
                            window.location.reload();
                        }else{
                            haha.$message.error(backData.msg);
                        }
                    });
                });
            },
            getDeptInfo(id){
                $.get("/demo/dept/getDept/"+id,function (backData) {
                    haha.getDept = backData.data;
                    haha.dsname = backData.data.dsname.state;
                    haha.docsname = backData.data.docsname.state;
                });
                haha.dialogFormVisible = true
            }
        }
    })
</script>
</body>
</html>